//
//  ShareDeviceViewController.m
//  jingdongfang
//
//  Created by mac on 16/9/8.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "ShareDeviceViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "ShareDeviceHeaderView.h"
#import "SearchDeviceListViewController.h"
#import "IGiGalleryAddSlaveUserApi.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "IGiGallerySearchUserApi.h"

@interface ShareDeviceViewController ()<UITextFieldDelegate, ABPeoplePickerNavigationControllerDelegate>

@property (nonatomic, strong) IGiGalleryAddSlaveUserApi *addUserApi;
@property (nonatomic, strong) ShareDeviceHeaderView *headView;

@end

@implementation ShareDeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"分享设备";
    self.title = NSLocalizedString(@"Sharing Device", nil);
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
    __weak typeof(self)weakSelf = self;
    _headView = [ShareDeviceHeaderView shareDeviceHeaderViewAddressBlock:^{
        IGAddressBookAuthorizationStatus status = [IGPermissionUtil addressAuthorizationStatus];
        if (status == IGAddressBookAuthorizationStatusNotDetermined) {//首次
            //请求通讯录权限
            [IGPermissionUtil requestAddressBookAuthorization:^{
                ABPeoplePickerNavigationController *peoplePickController = [[ABPeoplePickerNavigationController alloc] init];
                peoplePickController.peoplePickerDelegate = weakSelf;
                [weakSelf presentViewController:peoplePickController animated:YES completion:^{
                    
                }];
            } failureBlock:^{
                
            }];
        } else if (status == IGAddressBookAuthorizationStatusAuthorized) {//已授权
            ABPeoplePickerNavigationController *peoplePickController = [[ABPeoplePickerNavigationController alloc] init];
            peoplePickController.peoplePickerDelegate = weakSelf;
            [weakSelf presentViewController:peoplePickController animated:YES completion:^{
                
            }];
        } else {//拒绝使用通讯录
            [IGPermissionUtil ig_showAlertViewWithPermissionsAlertType:IGPermissionsAlertTypeAddressBook block:nil];
        }
        
    }];
    
    [self.view addSubview:_headView];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(60);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(181);
        
    }];
    _headView.textField.delegate = self;
    [_headView.textField becomeFirstResponder];
    
    // Do any additional setup after loading the view from its nib.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    __weak typeof(self) weakSelf = self;
    IGiGallerySearchUserApi *api = [[IGiGallerySearchUserApi alloc] initWithPhone:_headView.textField.text];
    [api startWithDecodeClass:[IGiGallerySearchUserResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGiGallerySearchUserResp *resp = responseObject;
            IGiGallerySearchUserModel *model = resp.data;
            
            if (!model.user) {
//                [weakSelf presentMessageTips:@"没有搜到该用户!"];
                [weakSelf presentMessageTips:NSLocalizedString(@"User not found!",nil)];
                return;
            }
            
            SearchDeviceListViewController * vc= [[SearchDeviceListViewController alloc]initWithNibName:@"SearchDeviceListViewController" bundle:nil];
            vc.macId = _macId;
            vc.userInfo = model.user;
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    }];
    return YES;
}

#pragma mark -ABPeoplePickerNavigationControllerDelegate

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

    ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFRelease(phoneNumbers);
    NSString* phoneNumber = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, ABMultiValueGetIndexForIdentifier(phoneNumbers, identifier));
    
    _headView.textField.text = phoneNumber;
    [_headView.textField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
